package org.peterbjornx.ui;

import org.peterbjornx.formats.RSInterface;

import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: PeterbjornX
 * Date: Apr 7, 2009
 * Time: 7:56:08 PM
 * Renders JAGeX interfaces to a given component and drawing area
 */
public class InterfaceRenderer {

    public int x;
    public int y;
    public int w;
    public int h;
    public InterfaceSource is;
    public Component c;
    public boolean debugging;

	public InterfaceRenderer (int _x,int _y,int _w,int _h,InterfaceSource _is,Component _c){
		x = _x;
		y = _y;
		w = _w;
		h = _h;
		c = _c;
		is = _is;
		debugging = false;
	}
	
    public void drawInterface(int id, int _x, int _y){
        _x+=x;
        _y+=y;
        Graphics g = c.getGraphics();
        RSInterface main = is.getInterface(id);
        if (main == null) System.err.println("Null interface");
        g.setClip(_x,_y,main.width,main.height);
        for (int cid = 0;cid < main.children.length;cid++){
            RSInterface child = is.getInterface(main.children[cid]);
            int cx = main.childX[cid]+_x;
            int cy = main.childY[cid]+_y;
            if ( ( cx >= w || cy >= h ) && !debugging )
                continue;
            if (child.type == 0)
                drawInterface(main.children[cid],cx-x,cy-y);
            if (child.type == 1)
                continue;
            if (child.type == 2){
                int iid = 0;
                for (int ix = 0;ix < child.width;ix++)
                    for (int iy = 0;iy < child.height;iy++)
                    {
                        int dx = ix * child.invSpritePadX;
                        int dy = iy * child.invSpritePadY;
                        dx+=cx;
                        dy+=cy+g.getFontMetrics().getHeight();
                        g.setColor(Color.WHITE);
                        g.drawString(child.inv[iid]+"\n"+child.invStackSizes[iid],dx,dy);
                        iid++;
                    }
            }
            if (child.type == 3){
                g.setColor(new Color(child.disabledColor));
                if (child.filled)
                    g.fillRect(cx,cy,child.width,child.height);
                else
                    g.drawRect(cx,cy,child.width,child.height);
            }
            if (child.type == 4){
                g.setColor(new Color(child.disabledColor));
                cy+=g.getFontMetrics().getHeight();
                String drawabletext = child.disabledText.replaceAll("\\n","\n");
                g.drawString(drawabletext,cx,cy);
            }
            if (child.type == 5){
                g.drawImage(child.disabledImage,cx,cy,c);
            }
            if (child.type == 6){
                g.setColor(Color.red);
                cy+=g.getFontMetrics().getHeight();
                g.drawString("N/A",cx,cy);
            }
            if (child.type == 7){
                int iid = 0;
                for (int ix = 0;ix < child.width;ix++)
                    for (int iy = 0;iy < child.height;iy++)
                    {
                        int dx = ix * child.invSpritePadX;
                        int dy = iy * child.invSpritePadY;
                        dx+=cx;
                        dy+=cy+g.getFontMetrics().getHeight();
                        g.setColor(Color.WHITE);
                        g.drawString(child.inv[iid]+"\n"+child.invStackSizes[iid],dx,dy);
                        iid++;
                    }
            }
            if (child.type == 8){
                g.setColor(new Color(0xFFFFE0));
                g.fillRect(cx,cy,child.width,child.height);
                g.setColor(Color.black);
                String drawabletext = child.disabledText.replaceAll("\\n","\n");
                g.drawString(drawabletext,cx,cy+g.getFontMetrics().getHeight());
            }
        }
    }

}
